﻿
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace TotalApplication.AngelSix
{
    /// <summary>
    /// a base class for value converters that can be used in XAML
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseValueConverter<T> : MarkupExtension, IValueConverter
        where T : class,new()
    {
        #region Private member
        private static T? _converter = null;

        /// <summary>
        /// Provides a static instance of the converter for use in XAML.
        /// </summary>
        /// <param name="serviceProvider">The service provider</param>
        /// <returns></returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return _converter ?? (_converter = new T());
        } 
        #endregion


        #region Value converter methods
        public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);


        public abstract object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
        
        #endregion
    }


    public abstract class ImitateValueConverter<T> : MarkupExtension,IValueConverter
        where T : class, new()
    {
        private static T? converter = null;


        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return converter??(converter = new T());
        }

        public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        public abstract object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
    }
}
